from flask import Flask, request, jsonify, render_template_string

app = Flask(__name__)

# HTML 模板
html_template = """
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Register</title>
</head>
<body>
    <h1>Register</h1>
    <form action="/register" method="post">
        <label for="email">邮箱:</label><br>
        <input type="email" id="email" name="email" required><br><br>

        <label for="username">用户名:</label><br>
        <input type="text" id="username" name="username" required><br><br>

        <label for="password">密码:</label><br>
        <input type="password" id="password" name="password" required><br><br>

        <label for="realname">真实姓名:</label><br>
        <input type="text" id="realname" name="realname" required><br><br>

        <label for="title">职称:</label><br>
        <select id="title" name="title" required>
            <option value="博士">博士</option>
            <option value="硕士">硕士</option>
            <option value="学生">学生</option>
            <option value="讲师">讲师</option>
            <option value="博士后">博士后</option>
            <option value="老师">老师</option>
            <option value="副教授">副教授</option>
            <option value="教授">教授</option>
            <option value="院士">院士</option>
        </select><br><br>

        <label for="baidu_key">百度翻译 Key:</label><br>
        <input type="text" id="baidu_key" name="baidu_key" required><br><br>

        <label for="serpapi_key">SerpAPI Key:</label><br>
        <input type="text" id="serpapi_key" name="serpapi_key" required><br><br>

        <button type="submit">注册</button>
    </form>
</body>
</html>
"""

# 空函数：测试百度翻译 Key
def test_baidu_key(key):
    # TODO: 实现百度翻译 Key 测试逻辑
    pass

# 空函数：测试 SerpAPI Key
def test_serpapi_key(key):
    # TODO: 实现 SerpAPI Key 测试逻辑
    pass

# 注册页面
@app.route('/register', methods=['GET'])
def register_page():
    return render_template_string(html_template)

# 注册接口
@app.route('/register', methods=['POST'])
def register():
    data = request.form
    email = data.get('email')
    username = data.get('username')
    password = data.get('password')
    realname = data.get('realname')
    title = data.get('title')
    baidu_key = data.get('baidu_key')
    serpapi_key = data.get('serpapi_key')

    # 测试百度翻译 Key 和 SerpAPI Key
    test_baidu_key(baidu_key)
    test_serpapi_key(serpapi_key)

    # 模拟保存用户信息
    user_info = {
        "email": email,
        "username": username,
        "password": password,
        "realname": realname,
        "title": title,
        "baidu_key": baidu_key,
        "serpapi_key": serpapi_key
    }

    return jsonify({"message": "注册成功", "user_info": user_info})

if __name__ == '__main__':
    ip = '0.0.0.0'
    port = 5000
    app.run(host=ip, port=port,debug=True)

# import json
# # from flask import Flask, jsonify
#
# # # 读取配置文件
# # def load_config(config_path='config.json'):
# #     with open(config_path, 'r', encoding='utf-8') as f:
# #         return json.load(f)
#
# # 初始化 Flask 应用
# def create_app(config):
#     app = Flask(__name__)
#     prefix = config['server']['prefix']
#
#     @app.route(f"{prefix}/status", methods=['GET'])
#     def status():
#         return jsonify({"status": "Server is running"})
#
#     @app.route(f"{prefix}/hello", methods=['GET'])
#     def hello():
#         return jsonify({"message": "Hello, World!"})
#
#     return app
#
# if __name__ == '__main__':
#     # 加载配置
#     # config = load_config()
#
#     # 创建 Flask 应用
#     # app = create_app(config)
#
#     # # 获取监听的 IP 和端口
#     # ip = config['server']['ip']
#     # port = config['server']['port']
#     #
#     # # 启动服务器
#     # app.run(host=ip, port=port)
#     print("This script is intended to be run as a module, not directly.")